/*******************************************************************************
 * Filename: team.js 
 *
 * This file contains common JavaScript funciton used throughout
 * the Fantasy Broomball website.  Many of the common functions 
 * are used to support frequent AJAX requests.
 *
 * Author: Phil Peterson
 * Date: 09/13/2009
 *
 ******************************************************************************/

var g_opposing_id = -1;
var g_myteam_id = -1;
var g_myteam_name = "";
var g_player_doc_id = "";

function swapRadio(elem){

    var radio = elem.childNodes[1].childNodes[0];
    radio.checked = true;
    
    var temp = radio.alt.split("_");
    var team_name = temp[0];
    var player_name = temp[1];
    
    //Set the name and player on the player image
    document.getElementById("trade_team_name_opp").innerHTML = team_name;
    document.getElementById("trade_player_name_opp").innerHTML = player_name;

}


function swapTradeButtonDown(elem){
    elem.style.cursor = "pointer";
    elem.style.backgroundImage = "url(images/trade/requesttrade_down.gif)";
}


function swapTradeButtonUp(elem){
    elem.style.backgroundImage = "url(images/trade/requesttrade_up.gif)";
}

/**
 * @Desc Function to swap non-transparent player image with the
 *       transparent version
 *
 * @param elem the HTML bench player element that was clicked
 *
 **/
function highlightTrade(elem){

	//Highlight the player selected
	elem.src = "images/rink/blue_player_down.png";
	var temp = elem.id.split("_");
	var id = temp[1];

	var label = document.getElementById("player_" + id + "_label");
	label.style.backgroundColor = "#E0F0F9";
	label.style.color = "#3A475A";

	elem.style.cursor = "pointer";

}


/**
 * @Desc Function to swap non-transparent player image with the
 *       transparent version
 *
 * @param elem the HTML bench player element that was clicked
 *
 **/
function unhighlightTrade(elem){

	if (g_myteam_id != elem.name ){
	    //Highlight the player selected
		elem.src = "images/rink/red_player_down.png";	
		var temp = elem.id.split("_");
		var id = temp[1];

		var label = document.getElementById("player_" + id + "_label");
		label.style.backgroundColor = "#E2AFAF";
		label.style.color = "#000000";
	}

}


function tradeClick(elem){

	g_myteam_id = elem.name;
	var temp = elem.alt.split("_");
	var team_name = temp[0];
	var player_name = temp[1];
    g_player_doc_id	= elem.id;
	
	
	g_myteam_name = player_name;
	
	var players = document.getElementsByClassName("players");
	for (var i = 0; i < players.length; i++)
		unhighlightTrade(players[i]);
    highlightTrade(elem);	
    
    //Set the name and player on the player image
    document.getElementById("trade_team_name_home").innerHTML = team_name;
    document.getElementById("trade_player_name_home").innerHTML = player_name;
}


function proposeTrade(){

	//Get the name and id
	var check_list = document.getElementsByName("trade_for");
	var player_id = -1;
	var opp_team = -1;
    var opp_name;
    var tempArr;
    
	for (var i = 0; i < check_list.length; i++){
		if (check_list[i].checked){
			tempArr = check_list[i].value.split("_");
			opp_team = tempArr[0];
			player_id = tempArr[1];
            opp_name = check_list[i].parentNode.parentNode.childNodes[2].childNodes[0].innerHTML;
            var index = i;
			break;
		}
	}

	if (g_myteam_id == -1)
		alert("Please select a player from your team trade");
	else if (player_id == -1)
		alert("Please select a player from another team to trade for");
		
	//Otherwise the trade can be completed
	else {   
    	//Prompt owner to confirm desire for trade
    	var response = confirm("Are you sure you want to trade " + g_myteam_name + " for " + opp_name);
    	
    	//Check if cancel was pressed
    	if (!response)
    	    return;
    	
	    //Need to add the test request into the trade table in the database. Use an HTTP request to send the
	    //request to the server
	    var type        = "addTrade";
	    var param_names = "type,team1,team2,player1,player2";
	    var param_vals  = type + "," + g_team_id + "," + opp_team + "," + g_myteam_id + "," + player_id;
	    var url         = "request_data.php";

	    response = postRequest(param_names, param_vals, url);
	
	    //Reset the rink players and the radio button
	    document.getElementById(g_player_doc_id).src = "images/rink/red_player_down.png";
        //Set the name and player on the player image
        document.getElementById("trade_team_name_home").innerHTML = "";
        document.getElementById("trade_player_name_home").innerHTML = "";
        document.getElementById("trade_team_name_opp").innerHTML = "";
        document.getElementById("trade_player_name_opp").innerHTML = "";        

	    var label = document.getElementById(g_player_doc_id + "_label");
	    label.style.backgroundColor = "#E2AFAF";
	    label.style.color = "#000000";  
	    
	    check_list[index].checked = "";
    }

}
